package com.sr.recitewords.exception;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.sr.recitewords.common.enums.ExpCodeEnum;
import io.swagger.annotations.ApiModelProperty;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 验证注解了required属性的为空的对象参数
 */
public class CheckNotNullUtil {

    /**
     * 验证对象必填参数不为空的方法
     *
     * @return
     */
    public static String getRequiredNotNullMsg(Object o) {
        //初始值(参数符合要求，必填项不为空)
        StringBuilder sb = new StringBuilder();
        //获取其所有属性包括父类属性
        Field[] fields = CheckNotNullUtil.getAllFields(o);
        //循环获取字段，拿注解ApiModelProperty  required属性为true的字段，然后验证其值不为空
        if (ObjectUtils.isNotEmpty(fields)) {
            for (Field field : fields) {
                //设置属性可见
                field.setAccessible(true);
                //判断是否具有ApiModelProperty
                boolean fieldHasAnno = field.isAnnotationPresent(ApiModelProperty.class);
                //具有则判断他是否必填
                if (fieldHasAnno) {
                    //是否必填,拿注解属性required
                    ApiModelProperty fieldAnno = field.getAnnotation(ApiModelProperty.class);
                    //如果是必填的
                    if (fieldAnno.required()) {
                        Object fieldValue = null;
                        try {
                            fieldValue = field.get(o);
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                        //如果属性值为空，则false
                        if (fieldValue == null || StringUtils.isBlank("" + fieldValue)) {
                            if (StringUtils.isNotBlank(fieldAnno.value())) {
                                sb.append("请填写" + fieldAnno.value());
                            } else {
                                sb.append(ExpCodeEnum.MISSING_REQUIRED_ARGUMENTS.getDesc());
                            }
                        } else if (fieldValue instanceof String && StringUtils.isBlank("" + fieldValue)) {
                            if (StringUtils.isNotBlank(fieldAnno.value())) {
                                sb.append("请填写" + fieldAnno.value());
                            } else {
                                sb.append(ExpCodeEnum.MISSING_REQUIRED_ARGUMENTS.getDesc());
                            }
                        }
                    }
                }
            }
        }
        return sb.toString();
    }

    public static boolean isRequiredNotNull(Object o) {
        String msg = getRequiredNotNullMsg(o);
        return StringUtils.isBlank(msg);
    }

    /**
     * 获取当前及其父类参数
     *
     * @param object
     * @return
     */
    private static Field[] getAllFields(Object object) {
        if (ObjectUtils.isNotEmpty(object)) {
            Class clazz = object.getClass();
            List<Field> fieldList = new ArrayList<>();
            while (clazz != null) {
                fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
                clazz = clazz.getSuperclass();
            }
            Field[] fields = new Field[fieldList.size()];
            fieldList.toArray(fields);
            return fields;
        }
        return null;
    }

    /*public static void main(String[] args) {
        HomeWorkPageReq req = new HomeWorkPageReq(1,10,null,null,null);

            System.out.println("当前对象信息：\n"+CheckNotNullUtil.getRequiredNotNullMsg(req));
            System.out.println("必选项是否不为空："+CheckNotNullUtil.isRequiredNotNull(req));


    }*/
}
